﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MoPhongLapLich
{
    class StartQueue : IAddQueue, IRunable
    {

        private LinkedList<Process> _processes;
        private List<IAddQueue> _link;
        private Tracker _tracker;
        public StartQueue(Tracker t)
        {
            Processes = new LinkedList<Process>();
            _link = new List<IAddQueue>();
            _tracker = t;

            CreateProcesses();
        }

        public LinkedList<Process> Processes
        {
            get { return _processes; }
            set { _processes = value; }
        }

        private void CreateProcesses()
        {
            Processes=new LinkedList<Process>();
            Process pro = new Process { Id = 0 }; //t=0
            List<Cycle> cycles=new List<Cycle>(){new Cycle(){Cpu  =10,R1 = 5},new Cycle{Cpu = 7,R2 = 6} };
            Processes.AddFirst(pro);
            pro.Cycles = cycles;

            pro = new Process { Id = 1 }; //t=2
            cycles = new List<Cycle>() { new Cycle() { Cpu = 7, R2 = 10 }, new Cycle() { Cpu = 8 } };
            Processes.AddFirst(pro);
            pro.Cycles = cycles;

            pro = new Process { Id = 2 };//t=5
            cycles = new List<Cycle> { new Cycle { Cpu = 9 }, new Cycle { Cpu = 10, R1 = 15 } };
            Processes.AddFirst(pro);
            pro.Cycles = cycles;

            pro = new Process { Id = 3 }; //t=6
            cycles = new List<Cycle> { new Cycle { Cpu = 8, R2 = 9 }, new Cycle { R1 = 10 } };
            Processes.AddFirst(pro);
            pro.Cycles = cycles;

            pro = new Process { Id = 4 }; //t=10
            cycles = new List<Cycle> { new Cycle { Cpu = 5, R2 = 7 }, new Cycle { Cpu = 3, R1 = 5 } };
            Processes.AddFirst(pro);
            pro.Cycles = cycles;

            _tracker.Processes = new List<Process>(Processes);
            
        }

        #region Implementation of IAddQueue

        public void Add(Process pro)
        {
            Processes.AddFirst(pro);
        }

        public void AddLink(IAddQueue link)
        {
            _link.Add(link);
        }

        #endregion

        #region Implementation of IRunable

        public void Run(object source, TickEventArgs e)
        {
            int tick = e.TickTime;
            Process pro = null;
            if (tick == 0) pro= Processes.Last.Value;
            else if (tick == 2) pro = Processes.Last.Value;
            else if (tick == 5) pro = Processes.Last.Value;
            else if (tick == 6) pro = Processes.Last.Value;
            else if (tick == 10) pro = Processes.Last.Value;

            if(pro!=null)
            {
                Processes.RemoveLast();
                foreach (var queue in _link)
                {
                    queue.Add(pro);
                }
            }
        }

        #endregion

        
    }
}
